package com.ss.android.action.a.c;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.bytedance.common.utility.UIUtils;
import com.ss.android.account.Spipe;
import com.ss.android.account.model.SharePlatform;
import com.ss.android.action.a.a.Comment;
import com.ss.android.article.browser.R;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.model.AbsArticle;
import com.ss.android.newmedia.a.DialogListener;
import com.ss.android.newmedia.a.WeakDialogListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public abstract class ShareDialog extends BaseShareDialog {
    public interface b$a {
        void a(Comment comment);
    }

    private View mNightModeOverlay;
    private boolean B;
    private long C = 0;
    private String D = null;
    private Activity mActivity;
    private boolean F;
    public int mArticleCommentMaxTextLength = 400;
    public boolean f = true;
    public b$a g;
    protected View mTitleBar;
    protected TextView mTitle;
    public EditText mShareText;
    protected View mShareBtn;
    protected View mBack;
    public TextView mLimitText;
    View mHeaderCover;
    InputMethodManager o;
    public AbsArticle p;
    long q = 0;
    public boolean r = false;
    public int s = -1;
    public long t = -1;
    public int u = 0;
    public boolean v;
    public Comment w;
    static String x = null;
    static String y = null;
    private DialogListener mDialogListener = new DialogListener() {
        public final void onCancel(DialogInterface arg1) {
        }

        public final void onDismiss(DialogInterface arg5) {
            if (ShareDialog.this.u != 0) {
                String v0 = ShareDialog.this.mShareText != null ? ShareDialog.this.mShareText.getText().toString() : null;
                if (android.support.design.a.isTextEmpty(v0)) {
                    return;
                }

                switch (ShareDialog.this.u) {
                    case 1: {
                        ShareDialog.x = com.bytedance.common.utility.aa.md5(String.valueOf(ShareDialog.this.p.groupId)) + "---" + v0;
                        break;
                    }
                    case 2: {
                        ShareDialog.y = com.bytedance.common.utility.aa.md5(String.valueOf(ShareDialog.this.q)) + "---" + v0;
                        break;
                    }
                }

                ShareDialog.this.u = 0;
            }
        }

        public final void onShow(DialogInterface arg2) {
            ShareDialog.this.tryRefreshTheme();
        }
    };

    public ShareDialog(Activity activity) {
        super(activity);
        this.mActivity = activity;
        this.B = true;
        WeakDialogListener dialogListener = new WeakDialogListener(this.mDialogListener);
        this.setOnShowListener(dialogListener);
        this.setOnDismissListener(dialogListener);
    }

    private String a(String arg5, AbsArticle arg6) {
        String v0_1;
        switch (this.u) {
            case 1: {
                if (arg6 == null) {
                    return arg5;
                }

                long v0 = arg6.groupId;
                Log.d("travis", "ShareDialog:::mSpipe=" + mSpipe);
                String avatarUrl = mSpipe.avatarUrl;
                v0_1 = com.bytedance.common.utility.aa.md5(String.valueOf(v0));
                if (android.support.design.a.isTextEmpty(avatarUrl)) {
                    return arg5;
                }

                if (!avatarUrl.startsWith(v0_1 + "---")) {
                    return arg5;
                }

                arg5 = avatarUrl.substring((v0_1 + "---").length());
                break;
            }
            case 2: {
                v0_1 = y;
                String v1 = com.bytedance.common.utility.aa.md5(String.valueOf(this.q));
                if (android.support.design.a.isTextEmpty(v0_1)) {
                    return arg5;
                }
                if (!v0_1.startsWith(v1 + "---")) {
                    return arg5;
                }
                arg5 = v0_1.substring((v1 + "---").length());
                break;
            }
        }

        return arg5;
    }

    public int getLayoutId() {
        return R.layout.ic;
    }

    protected final void a(int arg4) {
        if (arg4 == 105) {
            this.b();
            if (this.mSpipe != null) {
                this.mSpipe.a(true);
            }
        } else if (arg4 == 108) {
            this.b();
            if (this.mSpipe != null) {
                this.mSpipe.b(this.mContext);
            }
        }

        this.setCancelable(true);
        this.mHeaderCover.setVisibility(View.GONE);
        this.g();
        android.support.design.a.showImageToast(this.mContext, R.string.ss_post_fail, R.drawable.close_popup_textpage);
    }

    protected final void a(Activity arg2) {
        super.a(arg2);
        this.o = (InputMethodManager) arg2.getSystemService(Context.INPUT_METHOD_SERVICE);
    }

    public final void a(AbsArticle absArticle, long arg3, String arg5, long arg6) {
        this.q = arg6;
        this.C = arg3;
        this.p = absArticle;
        this.D = null;
        this.show();
        if (this.mShareText != null) {
            this.b(this.a(arg5, absArticle));
        } else {
            this.D = this.a(arg5, absArticle);
        }
    }

    public final void setHint(String hint) {
        if (this.mShareText != null) {
            this.mShareText.setHint(hint);
        }
    }

    private void b(String arg3) {
        if (arg3 == null) {
            arg3 = "";
        }

        if (arg3.length() > this.mArticleCommentMaxTextLength) {
            arg3 = arg3.substring(0, this.mArticleCommentMaxTextLength);
        }

        this.mShareText.setText(arg3);
        this.e();
    }

    protected final void b(Message message) {
        this.setCancelable(true);
        this.mHeaderCover.setVisibility(View.GONE);
        this.mShareText.setText("");
        this.g();
        if (this.isShowing()) {
            this.dismiss();
        }

        if (this.B) {
            android.support.design.a.showImageToast(this.mContext, R.string.ss_post_ok, R.drawable.doneicon_popup_textpage);
        }

        if (message.obj != null) {
            Object v0 = null;
            if ((message.obj instanceof Comment)) {
                v0 = message.obj;
            }

            if (message.arg1 == 108 && this.mSpipe != null) {
                this.mSpipe.b(this.mContext);
                if (v0 == null && this.mSpipe == null) {
                } else {
                    this.mSpipe.a(((Comment) v0).t, this.mContext);
                }
            }

            try {
                if (this.g == null) {
                    return;
                }

                if (v0 == null) {
                    return;
                }

                this.g.a(((Comment) v0));
            } catch (Exception v0_1) {
            }
        }
    }

    protected boolean c() {
        return true;
    }

    protected void tryRefreshTheme() {
        com.ss.android.j.b.isNightModeToggled();
        if ((this.c()) && this.mNightModeOverlay != null) {
            this.mNightModeOverlay.setVisibility(View.GONE);
        }

        if (this.mShareText != null) {
            this.mShareText.requestFocus();
            int v0 = android.support.design.a.isTextEmpty(this.mShareText.getText().toString()) ? 0 : this.mShareText.getText().length();
            EditText v2 = this.mShareText;
            if (this.r) {
                v0 = 0;
            }

            v2.setSelection(v0);
            this.o.showSoftInput(this.mShareText, 0);
        }

        if (!this.B && this.mTitle != null) {
            if (this.q > 0) {
                this.mTitle.setText(com.ss.android.article.browser.R.string.ss_title_comment_dialog_repost);
            } else {
                this.mTitle.setText(com.ss.android.article.browser.R.string.ss_title_comment_dialog);
            }
        }

        this.r = false;
    }

    public void e() {
        int v0 = this.mArticleCommentMaxTextLength - this.mShareText.getText().length();
        if (v0 < 0) {
            v0 = 0;
        }

        this.mLimitText.setText(String.valueOf(v0));
        this.g();
    }

    public void f() {
        if (this.p == null) {
            this.dismiss();
        } else if (!this.mSpipe.isLogin) {
            this.F = true;
            Bundle bundle = new Bundle();
            bundle.putString("extra_title_type", "title_post");
            bundle.putString("extra_source", "post_comment");
            bundle.putString("extra_from", "comment");
            Spipe.a(this.mActivity, bundle);
        } else {
            this.h();
        }
    }

    private void g() {
        boolean v0_1;
        boolean v1 = true;
        int v2 = 0;
        if (this.p == null) {
            this.mShareBtn.setEnabled(false);
        } else {
            SharePlatform[] v3 = this.PlatformItems;
            int v4 = v3.length;
            int v0;
            v0_1 = false;
            for (v0 = 0; v0 < v4; ++v0) {
                SharePlatform v5 = v3[v0];
                if ((v5.mLogin) && (v5.mSelected)) {
                    v0_1 = true;
                    break;
                }
            }


            if (this.mShareText.getText().toString().trim().length() > 0) {
                v2 = 1;
            }

            if (v2 == 0) {
                v1 = v0_1;
            }

            this.mShareBtn.setEnabled(v1);
        }
    }

    private void h() {
        int v2 = R.drawable.close_popup_textpage;
        Comment v3 = null;
        int v10 = 0;
        this.F = false;
        String v4 = this.mShareText.getText().toString();
        if (TextUtils.isEmpty(v4)) {
            this.mShareText.setText("");
            UIUtils.showImageToast(this.mContext, v2, R.string.ss_error_empty_content);
            return;
        }

        if (!NetworkUtils.isNetworkAvailable(this.mContext)) {
            UIUtils.showImageToast(this.mContext, v2, R.string.ss_comment_error_no_network);
            return;
        }

        this.o.hideSoftInputFromWindow(this.mShareText.getWindowToken(), 0);
        this.mHeaderCover.setVisibility(View.VISIBLE);
        this.setCancelable(false);
        String v8 = this.B ? "share" : "comment";
        if (this.w != null) {
            v4 = v4 + this.w.a(this.mSpipe);
            this.w = v3;
        }

        com.ss.android.action.a.b.a v0 = new com.ss.android.action.a.b.a(this.mContext, this.mHandler, ((List) null), v4, this.p, this.C, v8, this.B, 0, this.q);
        v0.a = 0;
        int v1 = this.s;
        long v4_1 = this.t;
        v0.read_pct = v1;
        v0.staytime_ms = v4_1;
        v0.d = this.v;
        v0.start();
        JSONObject v12 = new JSONObject();
        try {
            String v0_2 = "recommend_to_fans";
            if (this.v) {
                v10 = 1;
            }

            v12.put(v0_2, v10);
            v12.put("media_id", this.mSpipe.pgcMediaid);
            v12.put("uid", this.mSpipe.userId);
        } catch (JSONException v0_1) {
            v0_1.printStackTrace();
        }

        MobClickCombiner.onEvent(this.mContext, "xiangping", "write_confirm", this.p.groupId, this.p.itemId, v12);
        switch (this.u) {
            case 1: {
                x = ((String) null);
                return;
            }
            case 2: {
                y = ((String) null);
            }
        }

        return;


    }

    public void onAccountRefresh(boolean arg2, int arg3) {
        super.onAccountRefresh(arg2, arg3);
        if ((arg2) && (this.F)) {
            this.h();
        }
    }

    public void onBackPressed() {
        long v6 = 0;
        super.onBackPressed();
        long v4 = this.p != null ? this.p.groupId : v6;
        MobClickCombiner.onEvent(this.mContext, "comment", "write_cancel", v4, v6);
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        this.getWindow().setLayout(-1, -1);
        this.getWindow().setSoftInputMode(5);
        this.mNightModeOverlay = this.findViewById(com.ss.android.article.browser.R.id.night_mode_overlay);
        this.mHeaderCover = this.findViewById(com.ss.android.article.browser.R.id.ss_header_cover);
        this.mShareText = this.findViewById(com.ss.android.article.browser.R.id.ss_share_text);
        this.mLimitText = this.findViewById(com.ss.android.article.browser.R.id.ss_limit_text);
        this.mTitleBar = this.findViewById(com.ss.android.article.browser.R.id.title_bar);
        this.mShareBtn = this.findViewById(com.ss.android.article.browser.R.id.ss_share_btn);
        this.mBack = this.mTitleBar.findViewById(com.ss.android.article.browser.R.id.back);
        this.mTitle = this.findViewById(com.ss.android.article.browser.R.id.title);
        if (this.B) {
            this.mTitle.setText(com.ss.android.article.browser.R.string.ss_title_share_dialog);
        } else {
            this.mTitle.setText(com.ss.android.article.browser.R.string.ss_title_comment_dialog);
        }

        this.mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public final void onClick(View arg9) {
                long v6 = 0;
                long v4 = ShareDialog.this.p != null ? ShareDialog.this.p.groupId : v6;
                MobClickCombiner.onEvent(ShareDialog.this.mContext, "comment", "write_cancel", v4, v6);
                if (ShareDialog.this.mHeaderCover.getVisibility() != View.VISIBLE) {
                    ShareDialog.this.o.hideSoftInputFromWindow(ShareDialog.this.mShareText.getWindowToken(), 0);
                    ShareDialog.this.dismiss();
                }
            }
        });
        this.mShareBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public final void onClick(View arg2) {
                ShareDialog.this.f();
            }
        });
        if (!this.f) {
            this.mShareText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(this.mArticleCommentMaxTextLength)});
        }

        this.mShareText.addTextChangedListener(new TextWatcher() {
            public final void afterTextChanged(Editable arg3) {
                if (ShareDialog.this.f) {
                    int v0 = arg3.length();
                    if (v0 > ShareDialog.this.mArticleCommentMaxTextLength) {
                        arg3.delete(ShareDialog.this.mArticleCommentMaxTextLength, v0);
                    } else {
                        ShareDialog.this.e();
                    }
                } else {
                    ShareDialog.this.e();
                }
            }

            public final void beforeTextChanged(CharSequence arg1, int arg2, int arg3, int arg4) {
            }

            public final void onTextChanged(CharSequence arg1, int arg2, int arg3, int arg4) {
            }
        });
        try {
            String v0_1 = com.ss.android.action.bb.a().c;
            if (android.support.design.a.isTextEmpty(v0_1)) {
            } else {
                this.mShareText.setHint(((CharSequence) v0_1));
            }
        } catch (Exception v0) {
        }

        this.g();
        this.mHeaderCover.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public final boolean onTouch(View view, MotionEvent event) {
                return true;
            }
        });
        this.b(this.D);
        this.D = null;
    }
}

